package org.myorg.v4;

import java.util.Random;

public class FEvolution4 {
	/*
	 * 
	 * @param p_s 选择算子
	 * @param p_c 交叉算子 
	 * @param p_m 变异算子
	 */
	private double ps;
	private double pc;
	private double pm;
	
	
	public FEvolution4(double p_s, double p_c, double p_m) {
		ps = p_s;
		pc = p_c;
		pm = p_m;

	}

	/*
	 * 使用锦标赛的选择算法
	 */
/*	public void select(FPeople4 p) {
		for (int i = 0; i < p.p_num; i++) {
			if (i == 0 || i == 1) {
				p.putPerson(p.getBest(),i);
			} else {
				int pos1 = (int) (Math.random()*p.p_num);
				int pos2 = (int) (Math.random()*p.p_num);
				if (p.personAt(pos1).fitness > p.personAt(pos2).fitness)
					p.putPerson(p.personAt(pos1), i);
				else
					p.putPerson(p.personAt(pos2), i);
			}

		}
	}*/
	
	/*
	 * 转赌轮选择
	 */
	public void select(FPeople4 p) {
		
		FPeople4 np = new FPeople4(p.p_num);
		double[] pos = new double[p.p_num];
		double sum = 0;
		
		for (int i = 0; i < p.p_num; i++) {
			sum = sum + p.persons[i].fitness;
		}
		
		pos[0] = p.persons[0].fitness/sum;
		np.persons[0] = new FGene4(p.persons[0]);
		for (int i = 1; i < p.p_num; i++) {
			np.persons[i] = new FGene4(p.persons[i]);
			pos[i] = pos[i-1] +p.persons[i].fitness/sum;
		}
		
		for (int i = 0; i < p.p_num; i++) {
			double id = Math.random();
			for (int j = 0; j < p.p_num; j++){
				if (pos[j] > id) {
					p.persons[i] = new FGene4(np.persons[j]);
					break;
				}
			}
		}
		//p = np;

	}

	/*
	 * 单点交叉
	 */
/*	public void crossOver(FPeople4 p) {
		for (int i=0; i < (int) (pc*p.p_num); i++) {
			int pos1 = (int) (Math.random()*p.p_num);
			int pos2 = (int) (Math.random()*p.p_num);

			int cross_pos = (int) (Math.random()*p.getPersons()[0].length);
			
			String temp1 = p.getPersons()[pos1].gene.substring(0,cross_pos)
			+p.getPersons()[pos2].gene.substring(cross_pos);

			p.getPersons()[pos2].gene = p.getPersons()[pos2].gene.substring(0,cross_pos)
			+p.getPersons()[pos1].gene.substring(cross_pos);
			
			p.getPersons()[pos1].gene = temp1;

		}
	}*/
	
	public void crossOver(FPeople4 p) {
		for (int i=0; i < p.p_num-1; i++) {
			if (Math.random() < pc) {
				int cross_pos = (int) (Math.random()*p.getPersons()[0].length);
				
				String temp1 = p.getPersons()[i].gene.substring(0,cross_pos)
				+p.getPersons()[i+1].gene.substring(cross_pos);

				p.getPersons()[i+1].gene = p.getPersons()[i+1].gene.substring(0,cross_pos)
				+p.getPersons()[i].gene.substring(cross_pos);
				
				p.getPersons()[i].gene = temp1;
			}
		}
	}

	/*
	 * 单点变异
	 */
	public void mutation(FPeople4 p) {
		
		for(int i = 0; i < p.p_num; i++) {
//		int i = 0;
//		if(Math.random() > 0.5)
//			i = 1;
			for(int j = 0; j < p.getPersons()[0].length; j++) {
				if (Math.random() < pm) {
					char temp[]; 
					
					if (p.getPersons()[i].gene.charAt(j) == '0') {
						temp = p.getPersons()[i].gene.toCharArray();
						temp[j] = '1';
						p.getPersons()[i].gene = new String(temp);
					} else {
						temp = p.getPersons()[i].gene.toCharArray();
						temp[j] = '0';
						p.getPersons()[i].gene= new String(temp);
					}
				}
			}
		}
	}
}

